package com.ruoyi.web.controller.common;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.framework.config.WxCpConfiguration;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.service.IUnAuthService;
import com.ruoyi.system.service.SysLoginService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 验证码操作处理
 *
 * @author Lion Li
 */
@RestController
@AllArgsConstructor
@Slf4j
@RequestMapping("/unauth/")
public class UnAuthController {

    private final SysLoginService loginService;

    @GetMapping("/authorize")
    public String authorize(@RequestParam String code) throws WxErrorException {
        final WxCpService wxCpService = WxCpConfiguration.getCpService();
        WxCpOauth2UserInfo userInfo = wxCpService.getOauth2Service().getUserInfo(code);
        String token = loginService.qyLogin(userInfo.getUserId());
        String url ="http://emswx.kennede.com:49865/pages/sys/login/index?token="+token;
        System.out.println("-----------------------------------"+url);
        String s = HttpUtil.get(url);
        System.out.println(s);
        String html= "<!DOCTYPE html>\n" +
            " <meta http-equiv=\"refresh\" content=\"0;url= http://emswx.kennede.com:49865/pages/sys/login/index?token="+token+" \"><html>\n" +
            "  <head>\n" +
            "    <title>Hello World</title>\n" +
            "  </head>\n" +
            "  <body>\n" +
            "    <h1>Hello World!</h1>\n" +
            "    <div>\n" +
            "      <a href=\"http://emswx.kennede.com:49865/pages/sys/login/index?token="+token+"\">跳转</a>   \n" +
            "    </div>\n" +
            "  </body>\n" +
            "</html>";
        return html;
    }

    @GetMapping("/subject")
    public String subject(@RequestParam String code,@RequestParam String category) throws WxErrorException {
        final WxCpService wxCpService = WxCpConfiguration.getCpService();
        WxCpOauth2UserInfo userInfo = wxCpService.getOauth2Service().getUserInfo(code);
        String token = loginService.qyLogin(userInfo.getUserId());
        String url ="http://emswx.kennede.com:49865/pages/sys/login/subject?token="+token+"&category="+category;
        System.out.println("-----------------------------------"+url);
        String s = HttpUtil.get(url);
        System.out.println(s);
        String html= "<!DOCTYPE html>\n" +
            " <meta http-equiv=\"refresh\" content=\"0;url= http://emswx.kennede.com:49865/pages/sys/login/subject?token="+token+"&category="+category+" \"><html>\n" +
            "  <head>\n" +
            "    <title>Hello World</title>\n" +
            "  </head>\n" +
            "  <body>\n" +
            "    <h1>Hello World!</h1>\n" +
            "    <div>\n" +
            "      <a href=\"http://emswx.kennede.com:49865/pages/sys/login/subject?token="+token+"&category="+category+"\">跳转</a>   \n" +
            "    </div>\n" +
            "  </body>\n" +
            "</html>";
        return html;
    }

    @GetMapping("/category")
    public String category(@RequestParam String code,@RequestParam String category) throws WxErrorException {
        final WxCpService wxCpService = WxCpConfiguration.getCpService();
        WxCpOauth2UserInfo userInfo = wxCpService.getOauth2Service().getUserInfo(code);
        String token = loginService.qyLogin(userInfo.getUserId());
        String url ="http://emswx.kennede.com:49865/pages/sys/login/task?token="+token+"&category="+category;
        System.out.println("-----------------------------------"+url);
        String s = HttpUtil.get(url);
        System.out.println(s);
        String html= "<!DOCTYPE html>\n" +
            " <meta http-equiv=\"refresh\" content=\"0;url= http://emswx.kennede.com:49865/pages/sys/login/task?token="+token+"&category="+category+" \"><html>\n" +
            "  <head>\n" +
            "    <title>Hello World</title>\n" +
            "  </head>\n" +
            "  <body>\n" +
            "    <h1>Hello World!</h1>\n" +
            "    <div>\n" +
            "      <a href=\"http://emswx.kennede.com:49865/pages/sys/login/task?token="+token+"&category="+category+"\">跳转</a>   \n" +
            "    </div>\n" +
            "  </body>\n" +
            "</html>";
        return html;
    }
    @GetMapping("/toDetail")
    public String toDetail(@RequestParam String code,@RequestParam String id) throws WxErrorException {
        final WxCpService wxCpService = WxCpConfiguration.getCpService();
        WxCpOauth2UserInfo userInfo = wxCpService.getOauth2Service().getUserInfo(code);
        String token = loginService.qyLogin(userInfo.getUserId());
        String url ="http://emswx.kennede.com:49865/pages/sys/project/details?id="+id+"&token="+token;
        System.out.println("-----------------------------------"+url);
        String s = HttpUtil.get(url);
        System.out.println(s);
        String html= "<!DOCTYPE html>\n" +
            " <meta http-equiv=\"refresh\" content=\"0;url= http://emswx.kennede.com:49865/pages/sys/project/details?id="+id+"&token="+token+" \"><html>\n" +
            "  <head>\n" +
            "    <title>Hello World</title>\n" +
            "  </head>\n" +
            "  <body>\n" +
            "    <h1>Hello World!</h1>\n" +
            "    <div>\n" +
            "      <a href=\"http://emswx.kennede.com:49865/pages/sys/project/details?id="+id+"&token="+token+"\">跳转</a>   \n" +
            "    </div>\n" +
            "  </body>\n" +
            "</html>";
        return html;
    }
    @GetMapping("/toList")
    public String toList(@RequestParam String code,@RequestParam String id) throws WxErrorException {
        final WxCpService wxCpService = WxCpConfiguration.getCpService();
        WxCpOauth2UserInfo userInfo = wxCpService.getOauth2Service().getUserInfo(code);
        String token = loginService.qyLogin(userInfo.getUserId());
        String url ="http://emswx.kennede.com:49865/pages/sys/project/list-item?id="+id+"&token="+token;
        System.out.println("-----------------------------------"+url);
        String s = HttpUtil.get(url);
        System.out.println(s);
        String html= "<!DOCTYPE html>\n" +
            " <meta http-equiv=\"refresh\" content=\"0;url= http://emswx.kennede.com:49865/pages/sys/project/list-item?id="+id+"&token="+token+" \"><html>\n" +
            "  <head>\n" +
            "    <title>Hello World</title>\n" +
            "  </head>\n" +
            "  <body>\n" +
            "    <h1>Hello World!</h1>\n" +
            "    <div>\n" +
            "      <a href=\"http://emswx.kennede.com:49865/pages/sys/project/list-item?id="+id+"&token="+token+"\">跳转</a>   \n" +
            "    </div>\n" +
            "  </body>\n" +
            "</html>";
        return html;
    }

    @PostMapping(value = "/upload")
    public R<Void> upload(@RequestPart("file") MultipartFile file) {
        if (ObjectUtil.isNull(file)) {
            throw new ServiceException("上传文件不能为空");
        }
        String originalFilename = file.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");
        if(i < 1){
            return R.fail("文件类型无法识别");
        }
        String fileType = originalFilename.substring(i, originalFilename.length());
        String fileName = new Date().getTime() + fileType;
        File innerFile = new File("D:\\pms\\h5\\img\\"+fileName);
        try {
            file.transferTo(innerFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return R.ok(fileName);
    }


}
